/**
 * TManStopper.java
 * ************************** 
 * @date Jun 1, 2011
 * @author Michele Tamburini
 * 
 * **************************
 */
package infrastructureAPI.algorithms.instanceTools.facilities;

import gossipServices.basic.nodeDescriptors.ConcreteNodeDescriptor;
import gossipServices.bootstraping.tman.TManService;
import infrastructureAPI.Requester;
import infrastructureAPI.Service;
import infrastructureAPI.APIMessages.DispatcherRequestMessage;
import infrastructureAPI.APIMessages.RequestMessage;
import infrastructureAPI.algorithms.GeneralAPIFunctionAlgorithm;

import java.net.InetAddress;
import java.util.List;
import java.util.Vector;

/**
 * Description: 
 * The TManStopper class defines the algorithm to stop
 * a running TMan instance on a node.
 * It needs the Requester node in input to the 
 * constructor. 
 * It looks for the TManService in the target node.
 *
 */
public class TManStopper extends GeneralAPIFunctionAlgorithm {
	
	protected TManService tman;

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	protected Requester requester;
	
	public TManStopper(Requester requester){
		this.requester = requester;
	}

	@Override
	public RequestMessage createRequestMessage(InetAddress targetAddress,
			String targetNodeName) {
		return new DispatcherRequestMessage(
				new ConcreteNodeDescriptor(targetNodeName, targetAddress, null),
				this, requester);
	}

	@Override
	public Service extractServiceToDeRegister() {
		return tman;
	}

	@Override
	public Service extractSuppliedService() {
		return null;
	}

	@Override
	public boolean requireSystemDeRegistration() {
		return true;
	}

	@Override
	public boolean requireSystemRegistration() {
		return false;
	}

	@Override
	public List<Class<?>> requiredServices() {
		List<Class<?>> list = new Vector<Class<?>>();
		list.add(TManService.class);
		return list;
	}

	@Override
	public boolean requireReply() {
		return false;
	}

	@Override
	public void run() {
		tman = (TManService) services.get(0);
		tman.stopTMan();
		returnValue = true;
	}

}
